package com.push.spider.spider;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Splitter;
import com.push.common.config.TransConfig;
import com.push.common.manager.BeanManager;
import com.push.common.utils.DateUtils;
import com.push.common.utils.EncryptUtil;
import com.push.common.utils.MD5;
import com.push.common.utils.StringUtils;
import com.push.common.utils.httpclient.HttpClientUtil;
import com.push.spider.utils.UrlUtils;
import com.push.web.domain.BizDailyInfo;
import com.push.web.domain.BizSource;
import com.push.web.service.IBizDailyInfoService;
import com.push.web.service.IBizSourceService;
import com.rometools.rome.feed.synd.SyndEntry;
import com.rometools.rome.feed.synd.SyndFeed;
import com.rometools.rome.io.SyndFeedInput;
import com.rometools.rome.io.XmlReader;
import lombok.SneakyThrows;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import us.codecraft.webmagic.selector.Html;
import us.codecraft.webmagic.selector.Selectable;

import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DailyRssSpider {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private BizSource bizSource;

    @Autowired
    IBizSourceService sourceService = BeanManager.getBean(IBizSourceService.class);

    @Autowired
    IBizDailyInfoService articleService = BeanManager.getBean(IBizDailyInfoService.class);

    @Autowired
    private TransConfig transConfig = BeanManager.getBean(TransConfig.class);

    public int size = 0;

    public DailyRssSpider(String sourceId) {
        this.bizSource = sourceService.selectBizSourceBySourceId(Long.valueOf(sourceId));
    }

    @SneakyThrows
    public void run(){
        XmlReader reader = new XmlReader(new URL(bizSource.getSpiderUrl()));
        SyndFeed feed = new SyndFeedInput().build(reader);
        for (SyndEntry entry : feed.getEntries()) {
            String dataKey = EncryptUtil.encrypt(entry.getLink(), "md5");
            String title = entry.getTitle();
            String content = "";
            if (entry.getContents().size() > 0){
                content = entry.getContents().get(0).getValue();
            }
            String summary = "";
            if(null != entry.getDescription()){
                summary = entry.getDescription().getValue();
            }
            String publishTime = "", publishFormatTime = "";
            if (StringUtils.isNotNull(entry.getPublishedDate())){
                publishTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entry.getPublishedDate());
                publishFormatTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entry.getPublishedDate());
            }

            if (StringUtils.isEmpty(publishTime) && StringUtils.isNotNull(entry.getUpdatedDate())){
                publishTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entry.getPublishedDate());
                publishFormatTime = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, entry.getPublishedDate());
            }

            String url = entry.getLink();

            BizDailyInfo article = buildBizDailyInfo(dataKey,title, content, publishTime, publishFormatTime, url, bizSource.getSourceId(), bizSource.getInfoType());

            BizDailyInfo bizArticle = articleService.selectBizDailyDataKey(dataKey);
            if (StringUtils.isNull(bizArticle)){
                //自动翻译标题
                if (StringUtils.isContainsChinese(article.getTitleOrg())){
                    article.setTitleFor(article.getTitleOrg());
                } else {
                    String transWord = getTransResult(article.getTitleOrg(), "auto", "zh");
                    JSONObject titleObj = JSONObject.parseObject(transWord);
                    JSONArray titleArray = titleObj.getJSONArray("trans_result");
                    if (StringUtils.isNotNull(titleArray) && titleArray.size() > 0){
                        article.setTitleFor(titleArray.getJSONObject(0).getString("dst"));
                    }
                }
                //自动翻译摘要
                if(transConfig.isAbstractTrans() && StringUtils.isNotEmpty(article.getAbstractOrg())){
                    String transWord = getTransResult(article.getAbstractOrg(), "auto", "zh");
                    JSONObject titleObj = JSONObject.parseObject(transWord);
                    JSONArray titleArray = titleObj.getJSONArray("trans_result");
                    if (StringUtils.isNotNull(titleArray) && titleArray.size() > 0){
                        article.setAbstractFor(titleArray.getJSONObject(0).getString("dst"));
                    }
                }
                articleService.insertBizDailyInfo(article);
            } else {
                article.setId(bizArticle.getId());
                articleService.updateBizDailyInfo(article);
            }
            size++;
        }
    }

    public BizDailyInfo buildBizDailyInfo(String dataKey, String title, String summary, String publishTime,String publishFormatTime, String url, long sourceId, String infoType){
        BizDailyInfo article = new BizDailyInfo();
        article.setTitleOrg(addColor(title));
        article.setDataKey(dataKey);
        if (StringUtils.isNotEmpty(url)){
            article.setLink(url);
            String link = getParam(url,"url");
            if (StringUtils.isNotEmpty(link)){
                article.setLink(link);
            }
        }
        article.setAbstractOrg(addColor(summary));
        article.setAbstractFor(addColor(summary));
        article.setPublishOrg(publishTime);
        article.setPublishFor(publishFormatTime);
        article.setSourceId(sourceId);
        article.setCreateTime(DateUtils.getNowDate());
        article.setInfoType(infoType);
        return article;
    }

    public static String getParam(String url, String name) {
        String params = url.substring(url.indexOf("?") + 1, url.length());
        Map<String, String> split = Splitter.on("&").withKeyValueSeparator("=").split(params);
        return split.get(name);
    }

    public String getTransResult(String query, String from, String to) {
        Map<String, String> params = buildParams(query, from, to);
        return HttpClientUtil.doGet(transConfig.getTransUrl(), params,false).getContent();
    }

    private Map<String, String> buildParams(String query, String from, String to) {
        Map<String, String> params = new HashMap<String, String>();
        params.put("q", query);
        params.put("from", from);
        params.put("to", to);
        params.put("appid", transConfig.getAppid());
        // 随机数
        String salt = String.valueOf(System.currentTimeMillis());
        params.put("salt", salt);
        // 签名
        String src = transConfig.getAppid() + query + salt + transConfig.getSecurityKey();
        params.put("sign", MD5.md5(src));
        return params;
    }

    private static String addColor(String html){
        if (StringUtils.isEmpty(html)){
            return html;
        }
        Document doc = Jsoup.parse(html, "UTF-8");
        Elements imgList = doc.body().getElementsByTag("b");
        for(Element element : imgList){
            element.attr("style","color:#F00");
        }
        String newStr = doc.body().toString();
        return newStr.substring(6,newStr.length()-7).replaceAll("(\\r\\n|\\n|\\\\n)", "").trim();
    }

    public int getSize(){
        return size;
    }
}
